package com.dckj.auto.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dckj.auto.common.utils.StringUtil;
import com.dckj.auto.entity.AuthMenuRole;
import com.dckj.auto.dao.AuthMenuRoleDao;
import com.dckj.auto.service.AuthMenuRoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WangDong123
 * @since 2018-09-03
 */
@Service
public class AuthMenuRoleServiceImpl extends ServiceImpl<AuthMenuRoleDao, AuthMenuRole> implements AuthMenuRoleService {


    @Autowired
    AuthMenuRoleDao menuRoleDao;

    @Override
    public Integer insert(String[] menuIds, String roleId,Integer attribute) {
        List<String> strings = Arrays.asList(menuIds);
        Integer role_id = menuRoleDao.delete(new EntityWrapper<AuthMenuRole>().eq("role_id", roleId));
        Integer count = 0;
        for (int i = 0; i<menuIds.length; i++){
            AuthMenuRole menuRole = new AuthMenuRole();
            menuRole.setId(StringUtil.getUUId());
            menuRole.setMenuAttribute(attribute);
            menuRole.setMenuId(strings.get(i));
            menuRole.setRoleId(roleId);
            count += menuRoleDao.insert(menuRole);
        }
        if(count.equals(menuIds.length)){
            return 1;
        }
        return 0;
    }
}
